package pe.com.ValoracionVivienda.views.entrenamiento;

import java.util.List;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Tree;

import pe.com.ValoracionVivienda.domain.tipos.TipoValoracion;
import pe.com.ValoracionVivienda.service.impl.MenuArbolServiceImpl;
import pe.com.ValoracionVivienda.service.interfaces.MenuArbolService;
import pe.com.ValoracionVivienda.views.model.MenuItemArbol;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;

public class CompEntrenamientoPrincipal extends Composite {
	private Group group;
	private TreeViewer treeViewer;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public CompEntrenamientoPrincipal(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		
		SashForm sashForm = new SashForm(this, SWT.NONE);
		sashForm.setSashWidth(6);
		
		Tree tree = new Tree(sashForm, SWT.BORDER);
		treeViewer = new TreeViewer(tree);
		treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent arg0) {
				selectNodo();
			}
		});
		treeViewer.setContentProvider(new ITreeContentProvider() {
			@Override
			public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
				System.out.println("Now browsing: " + arg2);
			}
			
			@Override
			public void dispose() {
			}
			
			@Override
			public boolean hasChildren(Object element) {
				MenuItemArbol menuItemArbol = (MenuItemArbol)element;
				List<MenuItemArbol> menus = menuItemArbol.getItems();
				if(menus == null || menus.size() == 0)
					return false;
				return true;
			}
			
			@Override
			public Object getParent(Object element) {
				return ((MenuItemArbol)element).getItemPadre();
			}
			
			@Override
			public Object[] getElements(Object inputElement) {
				List<MenuItemArbol> items = ((MenuItemArbol)inputElement).getItems();
				if (items == null)
					return new Object[0];
				return items.toArray(new MenuItemArbol[0]);
			}
			
			@Override
			public Object[] getChildren(Object parentElement) {
				List<MenuItemArbol> items = ((MenuItemArbol)parentElement).getItems();
				if (items == null)
					return new Object[0];
				return items.toArray(new MenuItemArbol[0]);
			}
		});
		
		treeViewer.setLabelProvider(new LabelProvider(){
			public Image getImage(Object element){
				return null;
			}
			
			public String getText(Object element){
				return ((MenuItemArbol)element).getNombre();
			}
		});
		
		group = new Group(sashForm, SWT.NONE);
		FillLayout fl_group = new FillLayout(SWT.HORIZONTAL);
		fl_group.marginHeight = 5;
		group.setLayout(fl_group);
		sashForm.setWeights(new int[] {84, 360});
	}

	protected void selectNodo() {
		IStructuredSelection selection = (IStructuredSelection)treeViewer.getSelection();
		MenuItemArbol menuItem = (MenuItemArbol)selection.getFirstElement();
		
		if (menuItem.getNivel() == 1 && menuItem.getNombre().equalsIgnoreCase("autovaluo")){
			if (group.getChildren().length > 0)
				group.getChildren()[0].dispose();
			
			group.setText("Entrenamiento");
			CompEntrenamientos comp = new CompEntrenamientos(group, SWT.NONE);
			comp.initData(TipoValoracion.AUTOVALUO, null, null);
			group.layout(true);
		}
		
		if (menuItem.getNivel() == 2 && menuItem.getItemPadre().getNombre().equalsIgnoreCase("tasaciones")){
			if (group.getChildren().length > 0)
				group.getChildren()[0].dispose();
			
			group.setText("Entrenamiento");
			CompEntrenamientos comp = new CompEntrenamientos(group, SWT.NONE);
			comp.initData(TipoValoracion.TASACION, menuItem.getId(), menuItem.getNombre());
			group.layout(true);
		}
	}

	public void initData(){
		MenuArbolService menuArbolService = new MenuArbolServiceImpl();
		MenuItemArbol arbol = menuArbolService.getArbolEntrenamiento();
		treeViewer.setInput(arbol);
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
